const simpleGit = require("simple-git");
const fs = require("fs");
const path = require("path");

// 初始化 Git 仓库
let Repository = "/Volumes/ALL/test/node/nodegit";
const git = simpleGit(Repository + "/.git");

// 切换到指定目录

// console.log(git.cwd("/Volumes/ALL/test/node/nodegit/.git"));;

/**
 * @author 伟健 / 张
 * @name 提取接口
 */
function getInterface(_path) {
  fs.readFile(_path, "utf8", async (err, data) => {
    let apiList = JSON.parse(data.toString()).paths;

    for (let key in apiList) {
      if (key.indexOf("apis") == -1) {
        let data = apiList[key].post || apiList[key].get;
        let param = (data.parameters || []).map((item) => {
          try {
            return {
              name: item.name,
              type: item.schema ? item.schema.type : "",
              required: item.required,
              description: item.description,
            };
          } catch (e) {
            console.log("itemitemitem", item);
            console.log(e);
          }
        });
        if (param.length) {
          await new Promise((resolve) => {
            let str = JSON.stringify(
              {
                api: key,
                summary: data.summary,
                param,
              },
              "",
              "\t"
            );
            fs.writeFile(path.resolve(__dirname, "apis", `${data.summary}.json`), str, (err) => {
              resolve(1);
            });
          });
        }
      }
    }
  });
}

// getInterface(path.resolve(__dirname, "api-docs.json"));

// 获取当前分支

// git
//   .branch()
//   .then((branchInfo) => {
//     console.log("Current branch:", branchInfo.current);
//   })
//   .catch((error) => {
//     console.error("Error:", error);
//   });
git.cwd(Repository);
// 添加所有文件到暂存区
git
  .add(".")
  .then(() => {
    git.log({ maxCount: 1 }).then((log) => {
      let hash = log.all[0].hash;
      git.diff(["--unified=0", "--cached", hash]).then((diff) => {
        console.log("dd", diff);
      });
    });
  })
  .catch((error) => {
    console.error("Error:", error);
  });
git.cwd(Repository + ".git");
// e832f90164ed2f57580bf275ee299011a34f75c3

// // 提交更改
// git.commit('Initial commit')
//   .then(() => {
//     console.log('Commit created.');
//   })
//   .catch(error => {
//     console.error('Error:', error);
//   });

// // 推送更改到远程仓库
// git.push('origin', 'main')
//   .then(() => {
//     console.log('Changes pushed to remote repository.');
//   })
//   .catch(error => {
//     console.error('Error:', error);
//   });
